<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@include file="init.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/scrip.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>View Product Admin</title>
</head>
<body>
<div class="master-wrapper-content">
	<jsp:include page="heading.jsp"></jsp:include>
	<%
		System.out.println(session.getAttribute("roleId"));
		if(session.getAttribute("roleId") == null || String.valueOf(session.getAttribute("roleId")).equalsIgnoreCase("2")) {
			%>
				<h3 style="color: red">Không đủ quyền để truy cập</h3>
			<%
		}
		else {
	%>
			<jsp:include page="CategoryAdmin.jsp"></jsp:include>
			<div class="container-right">
			
					<% 
						List<String> categoryNames = new ArrayList<String>();
						List<String> producerNames = new ArrayList<String>();
						
						List<Product> products = dataManager.getAllProduct(categoryNames, producerNames);
					%>
					<div class="ml-banner">
						<div class="ml-title">Quản lý sản phẩm</div>
					</div>
					<table class="table-view">
						<tr class="title">
							<th>Name</th>
							<th>Date Updated</th>
							<th>Image</th>
							<th>Description</th>
							<th>Price</th>
							<th>Quantity</th>
							<th>Producer Name</th>
							<th>Category Name</th>
							<th>Edit</th>
							<th>Delete</th>
						</tr>
						<%
							int count = 0;
							for(int i = 0; i < products.size(); ++ i) {
								String str = "";
								++count;
								if (count % 2 == 0)
									str = "even";
								else
									str = "odd";
								
								Product product = products.get(i);
								String dateUpdated = "";
								if(product.getDateUpdated() != null) {
									Calendar cal = Calendar.getInstance();
									cal.setTime(product.getDateUpdated());
									dateUpdated = cal.get(Calendar.DATE) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.YEAR);
								}
								%>
								<tr class="<%=str%>">
									<td><%=product.getProductName() %></td>
									<td><%=dateUpdated %></td>
									<td>
										<img src="<%="/ECommerce/DisplayImage?path=" + product.getImage() %>" 
										style="width: 40px;height: 30px;" />
									</td>
									<%
										String description = product.getDescription();
										if(description.length() > 40) {
											
											int from = -2;
											int index;
											while(true) {
												index = description.indexOf("\r\n", from + 2);
												if(index == -1) {
													if(from == -2) {
														if(description.length() > 40) {
															from = description.indexOf(" ", 40);
															
															if(from == -1)
																from = description.length();
														}
														else
															from = description.length();
													}
													break;
												}
												
												if(index > 40) {
													from = description.indexOf(" ", 40);
													break;
												}
												from = index;
											}
											
											description = description.substring(0, from);
											description += "...";
										}
									%>
									<td><%=description.replace("\r\n", "<br />") %></td>
									<td><%=product.getPrice() %></td>
									<td><%=product.getQuantity() %></td>
									<td><%=producerNames.get(i) %></td>
									<td><%=categoryNames.get(i) %></td>
									<td><a href="<%="/ECommerce/EditProductAdmin.jsp?productId=" + product.getProductId() %>">Sửa</a></td>
									<td><a href="<%="/ECommerce/DeleteProduct?productId=" + product.getProductId() %>">Xóa</a></td>
								</tr>
								<%
							}
				%>
			</table>
			<br><br>
			<form action="/ECommerce/AddProductAdmin.jsp">
				<input type="submit" value="Add Product" />
			</form>
		</div>
		
		<div class="clear"></div>
	<%
			}
	%>
	<jsp:include page="footer.jsp"></jsp:include>
</div>
</body>
</html>